package com.dormitory.management.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dormitory.management.system.entity.Living;
import com.dormitory.management.system.req.LivingSaveReq;
import com.dormitory.management.system.req.LivingPageReq;
import com.dormitory.management.system.req.LivingUpdateReq;
import com.dormitory.management.system.service.LivingService;
import com.dormitory.managemnet.util.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 生活区表 前端控制器
 * </p>
 *
 * @author
 * @since 2021-12-17
 */
@Api(tags = "生活区管理")
@RestController
@RequestMapping("/living")
public class LivingController {
    @Autowired
    private LivingService livingService;

    @ApiOperation("添加生活区")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<String> save(@RequestBody LivingSaveReq req){
        if(!livingService.save(req)) {
            return Result.error("添加失败！");
        }
        return Result.success("添加成功！");
    }

    @ApiOperation("删除生活区")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public Result<String> delete(
            @ApiParam(value = "生活区id",required = true)
            @PathVariable Long id){
        if(!livingService.delete(id)) {
            return Result.error("删除失败！");
        }
        return Result.success("删除成功！");
    }

    @ApiOperation("更新生活区")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result<String> update(@RequestBody LivingUpdateReq req){
        if(!livingService.update(req)) {
            return Result.error("更新失败！");
        }
        return Result.success("更新成功！");
    }

    @ApiOperation("生活区分页列表")
    @RequestMapping(value = "/getPage",method = RequestMethod.POST)
    public Result<IPage<Living>> getPage(@RequestBody LivingPageReq req){
        return Result.ok(livingService.getPage(req));
    }

}
